package com.xhtt.modules.app.controller.msg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.msg.entity.MsgInfo;
import com.xhtt.modules.msg.entity.MsgUser;
import com.xhtt.modules.msg.service.MsgInfoService;
import com.xhtt.modules.msg.service.MsgUserService;
import com.xhtt.modules.sys.controller.AbstractController;
import com.xhtt.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author feipc
 * @since 2019-03-26
 */
@Api(tags = "消息", value = "消息")
@RestController
@RequestMapping("app/msg")
public class MsgInfoController extends AbstractController {

    @Autowired
    private MsgInfoService msgInfoService;

    @Autowired
    private MsgUserService msgUserService;

    /**
     * 获取app最新版本
     */
    @GetMapping(value = "/appVersion")
    @ApiOperation("获取app最新版本")
    @Login
    public R appVersion() {
        return R.ok().put(Constant.DATA, msgInfoService.appVersion());
    }

    /**
     * 获取消息表列表
     */
    @GetMapping(value = "/list")
    @ApiOperation("获取消息表列表")
    @Login
    public R msgList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        params.put("userId", user.getUserId());
        return R.ok().put(Constant.DATA, msgInfoService.msgList(params));
    }

    /**
     * 获取各大类消息的最新时间
     */
    @GetMapping(value = "/mapMaxDate")
    @ApiOperation("获取各大类消息的最新时间")
    @Login
    public R mapMaxDate(@ApiIgnore @LoginUser SysUserEntity user) {
        return R.ok().put(Constant.DATA, msgInfoService.mapMaxDate(user.getUserId().intValue()));
    }

    /**
     * 删除消息
     */
    @PostMapping(value = "/delete")
    @ApiOperation("删除消息")
    @Login
    public R delete(@RequestBody Integer[] ids, @ApiIgnore @LoginUser SysUserEntity user) {
        msgUserService.remove(new LambdaQueryWrapper<MsgUser>().in(MsgUser::getMsgId, Arrays.asList(ids)).eq(MsgUser::getUserId, user.getUserId()));
        return R.ok();
    }

    /**
     * 修改消息为已读
     */
    @PostMapping(value = "/read")
    @ApiOperation("修改消息为已读")
    @Login
    public R update(@RequestBody Integer[] ids, @ApiIgnore @LoginUser SysUserEntity user) {
        MsgUser msgUser = new MsgUser();
        msgUser.setStatus(1);
        msgUserService.update(msgUser, new LambdaQueryWrapper<MsgUser>().in(MsgUser::getMsgId, Arrays.asList(ids)).eq(MsgUser::getUserId, user.getUserId()));
        return R.ok();
    }

//    /**
//     * 获取消息表
//     */
//    @GetMapping(value = "/get/{id}")
//    @ApiOperation("获取消息表")
//    @Login
//    public R get(@PathVariable("id") Long id) {
//        return R.ok().put("", msgInfoService.getById(id));
//    }


    @GetMapping(value = "/sse")
    @ApiOperation("")
    public SseEmitter sse() {
        final SseEmitter sseEmitter = new SseEmitter(0l);
        Thread thread = new Thread(() -> {
            try {
                while (true) {
                    sseEmitter.send(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    Thread.sleep(3000);
//                    sseEmitter.complete();
                }
            } catch (Exception e) {
                sseEmitter.completeWithError(e);
            } catch (Error error) {
                sseEmitter.completeWithError(error);
            }
        });
        thread.start();


        return sseEmitter;
    }
}
